#include "StdAfx.h"
#include "ServiceInvoker.h"
#include "soapbdcSoapBindingProxy.h"
#include <tchar.h>
#include <atlconv.h>

using namespace bdc::service;

struct soap_cookie*	 pCookie;

LPSTR heapStr(LPCSTR pStr1)
{
	const int tagLen = strlen(pStr1);
	LPSTR pStr2 = new CHAR[tagLen + 1];
	strcpy(pStr2, pStr1);
	return pStr2;
}

ServiceInvoker::ServiceInvoker(void)
{
	char filePath[256];
	GetCurrentDirectoryA(256, filePath);
	strcat(filePath, "\\setup.ini");

	pServiceUrl = new char[256];
	GetPrivateProfileStringA("Server", "URL", "http://localhost:8080/services/bdc", pServiceUrl, 256, filePath);
}

ServiceInvoker::~ServiceInvoker(void)
{
}

BOOL ServiceInvoker::getTagList(vector<TagInfo*>& result)
{
	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__getTagList param;
	ns1__getTagListResponse response;

	int hr = proxy.getTagList(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		vector<ns1__tagInfo*> tagList = response.return_;

		for(int i = 0; i < tagList.size(); i++) {
			TagInfo* tagInfo = new TagInfo;
			
			tagInfo->tagId = tagList[i]->tagId;
			tagInfo->userId = tagList[i]->userId;
			tagInfo->tag = heapStr(tagList[i]->tag->c_str());
			tagInfo->amount = tagList[i]->amount;

			result.push_back(tagInfo);
		}

		return TRUE;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::getTagRecordList(LONG64 tagId, LONG64 from, LONG64 count, vector<RecordInfo*>& result)
{
	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__getTagRecordList param;
	ns1__getTagRecordListResponse response;

	param.tagId = tagId;
	param.from = from;
	param.count = count;

	int hr = proxy.getTagRecordList(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		vector<ns1__recordInfo*> recordList = response.return_;

		for(int i = 0; i < recordList.size(); i++) {
			RecordInfo* recordInfo = new RecordInfo;

			recordInfo->recordId = recordList[i]->recordId;
			recordInfo->userId = recordList[i]->userId;
			recordInfo->memory = heapStr(recordList[i]->memory->c_str());
			recordInfo->description = heapStr(recordList[i]->description->c_str());
			recordInfo->tag = heapStr(recordList[i]->tag->c_str());
			recordInfo->tagId = heapStr(recordList[i]->tagId->c_str());
			recordInfo->addTime = recordList[i]->addTime;

			result.push_back(recordInfo);
		}

		return TRUE;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::getTodayRecordList(vector<RecordInfo*>& result)
{
	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__getTodayRecordList param;
	ns1__getTodayRecordListResponse response;

	int hr = proxy.getTodayRecordList(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		vector<ns1__recordInfo*> recordList = response.return_;

		for(int i = 0; i < recordList.size(); i++) {
			RecordInfo* recordInfo = new RecordInfo;

			recordInfo->recordId = recordList[i]->recordId;
			recordInfo->userId = recordList[i]->userId;
			recordInfo->memory = heapStr(recordList[i]->memory->c_str());
			recordInfo->description = heapStr(recordList[i]->description->c_str());
			recordInfo->tag = heapStr(recordList[i]->tag->c_str());
			recordInfo->addTime = recordList[i]->addTime;

			result.push_back(recordInfo);
		}

		return TRUE;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::upateMemory(LONG64 recordId, int score)
{
	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__updateMemory param;
	ns1__updateMemoryResponse response;

	param.recordId = recordId;
	param.score = score;

	int hr = proxy.updateMemory(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::addRecord(LPCWSTR pMemory, LPCWSTR pDescription, LPCWSTR pTag)
{
	USES_CONVERSION;

	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__addRecord param;
	ns1__addRecordResponse response;

	string mstring(W2CA(pMemory));
	string dstring(W2CA(pDescription));
	string tstring(W2CA(pTag));

	param.memory = &mstring;
	param.description = &dstring;
	param.tag = &tstring;

	int hr = proxy.addRecord(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::updateRecord(LONG64 recordId, LPCWSTR pMemory, LPCWSTR pDescription, LPCWSTR pTag)
{
	USES_CONVERSION;

	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__updateRecord param;
	ns1__updateRecordResponse response;

	string mstring(W2CA(pMemory));
	string dstring(W2CA(pDescription));
	string tstring(W2CA(pTag));

	param.recordId = recordId;
	param.memory = &mstring;
	param.description = &dstring;
	param.tag = &tstring;

	int hr = proxy.updateRecord(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::delRecord(LONG64 recordId)
{
	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__delRecord param;
	ns1__delRecordResponse response;

	param.recordId = recordId;

	int hr = proxy.delRecord(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

LONG64 ServiceInvoker::addTag(LPCWSTR tagName)
{
	USES_CONVERSION;

	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__addTag param;
	ns1__addTagResponse response;

	string tag(W2CA(tagName));

	param.tag = &tag;

	int hr = proxy.addTag(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		return response.return_;
	}
	else {
		return -1;
	}
}

BOOL ServiceInvoker::updateTag(LONG64 tagId, LPCWSTR tagName)
{
	USES_CONVERSION;

	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__updateTag param;
	ns1__updateTagResponse response;

	string tag(W2CA(tagName));

	param.tagId = tagId;
	param.tag = &tag;

	int hr = proxy.updateTag(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::delTag(LONG64 tagId)
{
	bdcSoapBindingProxy proxy(pServiceUrl);
	proxy.cookies = pCookie;

	ns1__delTag param;
	ns1__delTagResponse response;

	param.tagId = tagId;

	int hr = proxy.delTag(&param, &response);
	proxy.cookies = NULL;

	if(hr == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

LONG64 ServiceInvoker::login(LPCWSTR mail, LPCWSTR password)
{
	USES_CONVERSION;

	bdcSoapBindingProxy proxy(pServiceUrl);

	ns1__login param;
	ns1__loginResponse response;

	string mstring(W2CA(mail));
	string pstring(W2CA(password));

	param.mail = &mstring;
	param.password = &pstring;

	if(proxy.login(&param, &response) == SOAP_OK) {
		pCookie = proxy.cookies;
		proxy.cookies = NULL;

		return response.return_;
	}
	else {
		return -2;
	}
}
